//
//  SLCertificationProcessItemComponent.m
//  SulitLoanDemo
//
//  Created by 虞嘉伟 on 2025/2/19.
//


#import "SLCertificationProcessItemComponent.h"
#import <YYKit.h>
#import <Masonry.h>
#import "DashedLineView.h"



@interface ProgressCircleView : UIView
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *circle;
@end

@implementation ProgressCircleView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = UIColor.whiteColor;
        self.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
        self.layer.borderWidth = 1;
        [self addSubview:self.titleLabel];
        [self addSubview:self.circle];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        [self.circle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self).inset(5);
        }];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.circle.layer.cornerRadius = CGRectGetHeight(_circle.bounds)*0.5;
    self.layer.cornerRadius = CGRectGetHeight(self.bounds)*0.5;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = UIColorHex(#1B1B1B);
        _titleLabel.font = aspectDefaultFont(14);
    }
    return _titleLabel;
}

- (UIView *)circle {
    if (!_circle) {
        _circle = [UIView new];
        _circle.backgroundColor = UIColorHex(#1B1B1B);
    }
    return _circle;
}
@end








@interface SLCertificationProcessItemComponent ()
@property (nonatomic, strong) DashedLineView *dashLine;
@property (nonatomic, strong) UIStackView *stackView;
@end
@implementation SLCertificationProcessItemComponent

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.bgView];
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        [self.bgView addSubview:self.dashLine];
        [self.dashLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.bgView).inset(16);
            make.centerY.equalTo(self.bgView);
            make.height.mas_equalTo(2);
        }];
        [self addSubview:self.stackView];
        [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.right.equalTo(self).inset(16);
        }];
    }
    return self;
}

- (void)setStepCount:(NSInteger)totalCount {
    _stepCount = totalCount;
    [self setNeedsDisplay];
}
- (void)setCurrentStep:(NSInteger)currentCount {
    _currentStep = currentCount;
    [self setNeedsDisplay];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _bgView.layer.cornerRadius = CGRectGetHeight(_bgView.bounds)*0.5;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    self.dashLine.hidden = _stepCount==0;
    [self.stackView removeAllSubviews];
    for (int i=0; i<_stepCount; i++) {
        ProgressCircleView *step = [ProgressCircleView new];
        step.titleLabel.text = @(i+1).stringValue;
        if (i < _currentStep) {
            step.circle.hidden = NO;
        } else {
            step.circle.hidden = YES;
        }
        [self.stackView addArrangedSubview:step];
        [step mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(27);
        }];
    }
}


- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = UIColorHex(#FDC5E0);
        _bgView.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
        _bgView.layer.borderWidth = 1.5;
    }
    return _bgView;
}
- (UIStackView *)stackView {
    if (!_stackView) {
        _stackView = [UIStackView new];
        _stackView.distribution = UIStackViewDistributionEqualCentering;
    }
    return _stackView;
}
- (DashedLineView *)dashLine {
    if (!_dashLine) {
        _dashLine = [DashedLineView new];
        _dashLine.hidden = YES;
    }
    return _dashLine;
}
@end
